package com.one.group.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.query.QueryWrapper;
import com.one.group.admin.mapper.SpChargeTypeMapper;
import com.one.group.admin.service.impl.SpChargeItemsServiceImpl;
import com.one.group.admin.service.impl.SpChargeTypeServiceImpl;
import com.one.group.model.dto.SpChargeTypeAddDto;
import com.one.group.model.dto.SpChargeTypeQueryDto;
import com.one.group.model.dto.SpChargeTypeUpdateDto;
import com.one.group.model.entity.SpChargeItems;
import com.one.group.model.entity.SpChargeType;
import com.one.group.model.vo.SpChargeTypeVo;
import com.one.group.result.Result;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.one.group.enums.ErrorCode;

import java.util.List;

@RestController
@RequestMapping("/spChargeType")
@Tag(name = "收费类型接口")
public class SpChargeTypeController {
    @Autowired
    private SpChargeTypeServiceImpl spChargeTypeService;
    @Autowired
    private SpChargeItemsServiceImpl spChargeItemsService;

    @Autowired
    private SpChargeTypeMapper spChargeTypeMapper;

    @PostMapping("/add")
    @Operation(summary = "添加收费类型")
    public Result add(@RequestBody SpChargeTypeAddDto spChargeTypeAddDto){
        return ResultUtil.success(spChargeTypeService.add(spChargeTypeAddDto));
    }
    @PostMapping("/list")
    @Operation(summary = "查询收费类型列表")
    public Result list(@RequestBody SpChargeTypeQueryDto spChargeTypeQueryDto){
        // 准备分页条件
        PageHelper.startPage(spChargeTypeQueryDto.getPage(),spChargeTypeQueryDto.getLimit());
        // 链式编程准备查询条件
        List<SpChargeTypeVo> spChargeTypeVos = spChargeTypeService.Querylist(spChargeTypeQueryDto);
        // 获取分页信息
        PageInfo<SpChargeTypeVo> pageInfo =new PageInfo<>(spChargeTypeVos);
        return ResultUtil.success(pageInfo);
    }
    @PutMapping("/update")
    @Operation(summary = "修改收费类型信息")
    public Result update(@RequestBody SpChargeTypeUpdateDto spChargeTypeUpdateDto){
        Long id = spChargeTypeUpdateDto.getId();
        String name = spChargeTypeUpdateDto.getName();
        String remarks = spChargeTypeUpdateDto.getRemarks();
        SpChargeType spChargeType = spChargeTypeService.getById(id);
        spChargeType.setName(name);
        spChargeType.setRemarks(remarks);
        Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
        return ResultUtil.success(spChargeTypeService.updateById(spChargeType));
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除收费项类型")
    public Result delete(Long  id){
        try{
            //查询收费项目是否有绑定
            List<SpChargeItems> list = spChargeItemsService.list(new QueryWrapper().eq("chargetype_id",id));
            if(list.size()>0){
                return ResultUtil.error(ErrorCode.OPERATION_ERROR);
            }
            //删除
            return ResultUtil.success(spChargeTypeService.removeById(id));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/findAll")
    @Operation(summary = "查询所有收费类型")
    public Result findAll(){
        return ResultUtil.success(spChargeTypeService.findAll());
    }
}
